﻿#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

//float dist2_points(float x_1, float y_1, float x_2, float y_2);
//int judge_intersection(float x_1, float y_1, float radius_1, float x_2, float y_2, float radius_2);
bool judge_intersection(int x_1, int y_1, int radius_1, int x_2, int y_2, int radius_2);
void draw_circle1(float x, float y, float radius);
void draw_circle2(float x, float y, float radius);
void draw_circle3(float x, float y, float radius);
void draw_circle4(float x, float y, float radius);


int main()
{
	int width = 600;
	int height = 600;
	initgraph(width, height);
	setbkcolor(WHITE);
	cleardevice();
	srand(time(0));

	int x_array[1000];
	int y_array[1000];
	int radius_array[1000];
	int radius_min = 8;
	int radius_max = 50;
	int ball_num;
	float x;
	float y;
	float radius = 8;
	int judge;
	int i = 0;
	int j = 0;
	int draw_mode = 3;

	for (ball_num = 0; ball_num < 1000; ball_num++)
	{
		judge = 0;

		while (judge == 0)
		{
			if (_kbhit())
			{
				char input = _getch();
				if (input == ' ')
				{
					ball_num = 0;

					cleardevice();
					draw_mode += 1;
					if (draw_mode > 4)
					{
						draw_mode = 1;
					}
				}
			}

			x = rand() % width;
			y = rand() % height;
			radius = radius_min;

			for (i = 0; i < ball_num; i++)
			{
				if (judge_intersection(x_array[i], y_array[i], radius_array[i], x, y, radius))
				{
					break;
				}
			}

			if (i == ball_num)
			{
				judge = 1;
			}
		}

		judge = 0;
		while (judge == 0 && radius < radius_max)
		{
			radius++;
			for (j = 0; j < ball_num; j++)
			{
				if (judge_intersection(x_array[j], y_array[j], radius_array[j], x, y, radius))
				{
					judge = 1;
					break;
				}
			}
		}

		x_array[ball_num] = x;
		y_array[ball_num] = y;
		radius_array[ball_num] = radius;

		if (draw_mode == 1)
		{
			draw_circle1(x, y, radius);
		}

		if (draw_mode == 2)
		{
			draw_circle2(x, y, radius);
		}

		if (draw_mode == 3)
		{
			draw_circle3(x, y, radius);
		}

		if (draw_mode == 4)
		{
			draw_circle4(x, y, radius);
		}

		Sleep(10);
	}

	int c = _getch();
	closegraph();
	return 0;
}

/*
float dist2_points(float x_1, float y_1, float x_2, float y_2)
{
	float dist2 = sqrt(x_1 - x_2) * (x_1 - x_2) + (y_1 - y_2) * (y_1 - y_2);
	return dist2;
}


int judge_intersection(float x_1, float y_1, float radius_1, float x_2, float y_2, float radius_2)
{
	if (dist2_points(x_1, y_1, x_2, y_2) < radius_1 + radius_2)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
*/

bool judge_intersection(int x_1, int y_1, int radius_1, int x_2, int y_2, int radius_2)
{
	float dist2 = (x_1 - x_2) * (x_1 - x_2) + (y_1 - y_2) * (y_1 - y_2);
	float radius2 = (radius_1 + radius_2) * (radius_1 + radius_2);
	if (dist2 < radius2)
	{
		return true;
	}
	else
	{
		return false;
	}
}


void draw_circle1(float x, float y, float radius)
{
	setlinecolor(BLACK);
	setfillcolor(YELLOW);
	fillcircle(x, y, radius);
}


void draw_circle2(float x, float y, float radius)
{
	float h = rand() % 360;
	COLORREF color = HSVtoRGB(h, 0.6, 0.8);
	setlinecolor(WHITE);
	setfillcolor(color);
	fillcircle(x, y, radius);
}


void draw_circle3(float x, float y, float radius)
{
	while (radius > 0)
	{
		float h = rand() % 360;
		COLORREF color = HSVtoRGB(h, 0.6, 0.8);
		setlinecolor(WHITE);
		setfillcolor(color);
		fillcircle(x, y, radius);
		radius -= 5;
	}
}


void draw_circle4(float x, float y, float radius)
{
	while (radius > 0)
	{
		float h = rand() % 360;
		COLORREF color = HSVtoRGB(h, 0.9, 0.8);
		setlinecolor(color);
		circle(x, y, radius);
		radius -= 5;
	}
}